package com.dundun.SSO.controller.manage;

import com.dundun.SSO.core.base.BaseController;
import com.dundun.SSO.manage.ISsoRoleService;
import com.dundun.SSO.manage.pojo.SsoRole;
import com.dundun.SSO.manage.query.SsoRoleQuery;
import com.dundun.SSO.pojo.ResponseBean;
import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;

/**********************************************************
 * 版权所有：搜影科技 保留所有权利
 * 创建日期: 2017/5/15 0015 15:36
 * 创建作者: gezhangkai
 * 版    本:
 * 功    能:
 * 最后修改时间:
 * 修改记录:
 ***********************************************************/
@Controller
@RequestMapping("/sso/role")
public class SsoRoleController extends BaseController {
    Logger logger = LoggerFactory.getLogger(this.getClass());
    
    @Autowired
    private ISsoRoleService ssoRoleService;

    public String getPage(){
        return "ssoRole";
    }

    @RequestMapping("/qryList.json")
    @ResponseBody
    public ResponseBean getSsoRoleList(@RequestBody SsoRoleQuery ssoRoleQuery){
        ResponseBean responseBean = new ResponseBean();
        try {
            List<SsoRole> ssoRoleList = ssoRoleService.selectByPage(ssoRoleQuery);
            int count = ssoRoleService.selectCountByPage(ssoRoleQuery);
            responseBean.setRows(ssoRoleList);
            responseBean.setTotal(count);
        }catch (Exception e){
            logger.error("getSsoRoleList",e);
        }finally {
            return responseBean;
        }
    }

    @RequestMapping("update.json")
    @ResponseBody
    public ResponseBean updateSsoRole(@RequestBody SsoRole ssoRole){
        ResponseBean responseBean = new ResponseBean();
        try{
            if(ssoRole.getId() == null){
                return ResponseBean.parameterError("id不能为空");
            }
            if(StringUtils.isBlank(ssoRole.getName())){
                return ResponseBean.parameterError("名称不能为空");
            }
            if(ssoRole.getParentId() != null){
                if(ssoRole.getParentId() == ssoRole.getId()){
                    return ResponseBean.parameterError("不能继承自身");
                }
                if(ssoRoleService.selectAllParentRole(ssoRole.getParentId()).contains(ssoRole.getId())){
                    return ResponseBean.parameterError("不能继承下级");
                }
            }
            if(ssoRoleService.updateByPrimaryKey(ssoRole)>0){
                return ResponseBean.success(null,null);
            }else{
                return ResponseBean.unknowError(null,null);
            }
        }catch (Exception e){
            logger.error("updateSsoRole",e);
        }
        return responseBean;
    }

    @RequestMapping("add.json")
    @ResponseBody
    public ResponseBean addSsoRole(@RequestBody SsoRole ssoRole){
        ResponseBean responseBean = new ResponseBean();
        try{
            if(StringUtils.isBlank(ssoRole.getName())){
                return ResponseBean.parameterError("名称不能为空");
            }
            ssoRoleService.saveNotNull(ssoRole);
        }catch (Exception e){

        }
        return responseBean;
    }

    @RequestMapping("delete.json")
    @ResponseBody
    public ResponseBean deleteSsoRole( Long[] ids){
        ResponseBean responseBean = new ResponseBean();
        List<Long> notDelete = new ArrayList<Long>();
            for(Long id:ids){
                SsoRole ssoRole1 = ssoRoleService.selectByPrimaryKey(id);
                if(null == ssoRole1){
                    continue;
                }
                List<Long> idList = new ArrayList<Long>();
                idList.add(id);
                List<SsoRole> ssoRoleList = ssoRoleService.selectAllSubRole(id);
                for(SsoRole ssoRole:ssoRoleList){
                    idList.add(ssoRole.getId());
                }
               boolean b = ssoRoleService.deleteList(idList);
                if(b){
                    notDelete.add(id);
                }
            }
        if(notDelete.size()>0){
            String msg = String.format("id为%s的数据未能删除",notDelete);
            return ResponseBean.success(msg,null);
        }
        return ResponseBean.success("",null);
    }
}
